﻿using System.IO;
using Foundation;
using SQLite.Net;
using SQLite.Net.Platform.XamarinIOS;
using UIKit;
using Xamarin.Forms;
using XLabs.Caching;
using XLabs.Caching.SQLite;
using XLabs.Forms;
using XLabs.Forms.Controls;
using XLabs.Ioc;
using XLabs.Platform.Device;
using XLabs.Platform.Mvvm;
using XLabs.Platform.Services;
using XLabs.Serialization;
using XLabs.Serialization.ServiceStack;

using Foundation;
using HalalCheck;
using UIKit;
using Xamarin;

namespace HalalCheckiOS
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS.
    [Register("AppDelegate")]
    public partial class AppDelegate : XFormsApplicationDelegate
    {
        // class-level declarations
        UIWindow window;

        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
#if DEBUG
                        Insights.Initialize(Insights.DebugModeKey);
#else
            Insights.Initialize("04573c51cc5fcc1229c321005ddf96fef42b2555");
#endif
            // create a new window instance based on the screen size
            this.SetIoc();

            new CalendarViewRenderer(); //added so the assembly is included

            Forms.Init();
            var formsApp = new App();

            LoadApplication(formsApp);

            //			this._window = new UIWindow(UIScreen.MainScreen.Bounds)
            //			{
            //				RootViewController = App.GetMainPage().CreateViewController()
            //			};

            Forms.ViewInitialized += (sender, e) =>
            {
                if (!string.IsNullOrWhiteSpace(e.View.StyleId))
                {
                    e.NativeView.AccessibilityIdentifier = e.View.StyleId;
                }
            };


            base.FinishedLaunching(app, options);

            return true;
        }

        /// <summary>
        /// Sets the IoC.
        /// </summary>
        private void SetIoc()
        {
            var resolverContainer = new SimpleContainer();

            var app = new XFormsAppiOS();
            app.Init(this);

            var documents = app.AppDataDirectory;
            var pathToDatabase = Path.Combine(documents, "halalcheck.db");
            var sql = new SQLitePlatformIOS();
            var sqlconnectionstring = new SQLiteConnectionString(pathToDatabase, true);

            resolverContainer.Register<IDevice>(t => AppleDevice.CurrentDevice)
                .Register<IDisplay>(t => t.Resolve<IDevice>().Display)
                .Register<IJsonSerializer, JsonSerializer>()
                // .Register<IJsonSerializer, Services.Serialization.SystemJsonSerializer>()
                .Register<IXFormsApp>(app)
                .Register<INetwork, Network>()
                .Register<ISecureStorage, SecureStorage>()
                .Register<IDependencyContainer>(t => resolverContainer)

                .Register<ISimpleCache>(
                    t => new SQLiteSimpleCache(sql, sqlconnectionstring, t.Resolve<IJsonSerializer>()));
            // .Register<IAsyncSimpleCache>(t => new SQLiteSimpleCache(sql, sqlconnectionstring, t.Resolve<IJsonSerializer>()));

            Resolver.SetResolver(resolverContainer.GetResolver());
        }
    }
}